<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <!-- 按钮区域 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <el-table :data="roleList" border stripe style="width: 100%">
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row :class="['bdbottom', i1 === 0 ? 'bdtop' : '', 'vcenter']" v-for="(item1, i1) in scope.row.children"
              :key="item1.id">
              <!-- 一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="removeRightById(item1.id)">{{ item1.roleName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级以及三级权限 -->
              <el-col :span="19">
                <el-row :class="[i2 === 0 ? '' : 'bdtop', 'vcenter']" v-for="(item2, i2) in item1.children"
                  :key="item2.id">
                  <!-- 二级权限 -->
                  <el-col :span="6">
                    <el-tag type="success" closable @close="removeRightById(item2.id)">{{ item2.roleName }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 三级权限 -->
                  <el-col :span="18">
                    <el-tag type="warning" v-for="(item3, i3) in item2.children" :key="item3.id" closable
                      @close="removeRightById(item3.id)">
                      {{ item3.roleName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <!-- 索引列 -->
        <el-table-column type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column label="操作" width="300px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            <el-button type="warning" icon="el-icon-setting" size="mini" @click="showSetRightDialog(scope.row)">分配权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限对话框 -->
    <el-dialog title="提示" :visible.sync="setRightDialogVisible" width="50%" @close="setRightDialogClosed">
      <!-- 树形控件 -->
      <el-tree :data="rightsList" :props="treeProps" show-checkbox node-key="id" default-expand-all
        :default-checked-keys="defKeys"></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRightDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
 
<script>
export default {
  name: 'Roles',
  data() {
    return {
      roleList: [],  // 权限列表
      setRightDialogVisible: false,  // 控制分配权限对话框的展示与否
      rightsList: [],  // 树状权限数据
      // 树形控件树形绑定
      treeProps: {
        label: 'authName',
        children: 'children'
      },
      // 默认选中的节点 id 值
      defKeys: [],
    }
  },
  created() {
    // 获取权限列表
    this.getRolesList()
  },
  methods: {
    // 获取权限列表的函数
    async getRolesList() {
      const { data: res } = await this.$ajax.get('permission/roles/list')
      if (res.meta.code !== 200) return this.$message.error('获取权限列表失败！')
      this.roleList = res.data
    },
    // 根据 id 删除对应权限
    async removeRightById(id) {
      const confirmResulse = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err);
      if (confirmResulse !== 'confirm') {
        return this.$message.info('删除操作已取消！')
      }
      const { data: res } = await this.$ajax.delete('permission/roles/' + id)
      if (res.meta.code !== 200) return this.$message.error('权限删除失败！')
      // todo:刷新权限列表
      // this.getRolesList() - 会导致页面刷新
      // 为角色信息重新赋值
      this.$message.success('已删除！')
    },
    // 展示分配权限对话框
    async showSetRightDialog(role) {
      const { data: res } = await this.$ajax.get('permission/tree')
      if (res.meta.code !== 200) return this.$message.error('权限列表获取失败!')
      this.rightsList = res.data
      this.getLeafKeys(role, this.defKeys)
      this.setRightDialogVisible = true
    },
    // 获取三级权限id保存到defKey数组中
    getLeafKeys(node, arr) {
      // 如果是三级节点，不包含children属性
      if (!node.children) {
        return arr.push(node.id)
      }

      node.children.forEach(element => {
        this.getLeafKeys(element, arr)
      });
    },
    // 监听 分配权限对话框的 关闭事件
    setRightDialogClosed() {
      this.defKeys = []
    }
  }
}
</script>
 
<style lang='less' scoped>
.vcenter {
  display: flex;
  align-items: center;
}
</style>